/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string>

#include "yaffut.h"
#include "myassert.h"
#include "TestEventServer.h"
#include "TestSession.h"

#include "CommServer.h"
#include "CommChannel.h"

using namespace Chronos;
using namespace std;

struct CommServerTest {};


TEST(CommServerTest, testShouldRegisterAtEventServer) {
	TestEventServer eventServer;
	CommServer* commServer = 0;
	{
		CommServer server(eventServer);
		commServer = &server;
    	ASSERT_NOT_NULL(eventServer.regListener);
	    ASSERT_TRUE(eventServer.regListener == commServer);
	}
    ASSERT_TRUE(eventServer.unregListener == commServer);
}



struct CommServerTest2 {
	CommServerTest2() : session1(""), session2(""), commServer(eventServer)
    {
	    commServer.addChannel("channel1", ANSI_BLUE);
    	commServer.addChannel("channel2", ANSI_RED);
    }

    TestSession session1;
    TestSession session2;
    TestEventServer eventServer;
    CommServer commServer;
};


TEST(CommServerTest2, testShouldAddPlayerToChannelsAtLogin) {
    commServer.notify(Event::PLAYER_LOGIN, session1);
    session1.SESSION_CHECKWRITTEN("You joined the ["ANSI_BLUE"channel1"ANSI_NORMAL"] channel.\n"
            "You joined the ["ANSI_RED"channel2"ANSI_NORMAL"] channel.\n");
}


TEST(CommServerTest2, testShouldRemovePlayerFromChannelsAtLogout) {
    commServer.notify(Event::PLAYER_LOGIN, session1);
    session1.clear();

    commServer.notify(Event::PLAYER_LOGOUT, session1);
    session1.SESSION_CHECKWRITTEN("You left the ["ANSI_BLUE"channel1"ANSI_NORMAL"] channel.\n"
           "You left the ["ANSI_RED"channel2"ANSI_NORMAL"] channel.\n");

    commServer.notify(Event::PLAYER_LOGOUT, session1);
    session1.SESSION_CHECKWRITTEN("");

    commServer.join(session1, "channel1");
    session1.clear();
    commServer.notify(Event::PLAYER_LOGOUT, session1);
    session1.SESSION_CHECKWRITTEN("You left the ["ANSI_BLUE"channel1"ANSI_NORMAL"] channel.\n");
}


TEST(CommServerTest2, testShouldOnlyAddPlayerToPermanentChannels) {
    commServer.addPlayerChannel(session2, "mychannel1", ANSI_GREEN);
    commServer.addPlayerChannel(session2, "mychannel2", ANSI_YELLOW);
    session1.clear();

    commServer.notify(Event::PLAYER_LOGIN, session1);
    session1.SESSION_CHECKWRITTEN("You joined the ["ANSI_BLUE"channel1"ANSI_NORMAL"] channel.\n"
            "You joined the ["ANSI_RED"channel2"ANSI_NORMAL"] channel.\n");
}

